home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / WIN_PRO / WNDLIB10.ZIP;1 / WNDEX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-25  |  6.5 KB  |  233 lines

  1. #include <wnddb.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #include "wndex.h"
  6.  
  7. const unsigned char faces[6] = { 4, 6, 8, 10, 12, 20 };
  8.  
  9. unsigned char test = 0, die = 6, dice = 2;
  10. unsigned int times = 100, rolls[100];
  11.  
  12. INSTANCE_CLASS hinst;
  13.  
  14. DBFUNC RunTest(HWND hwnd, WORD message, WORD wparam, LONG lparam){
  15.   int i, j;
  16.   char string[3] = "  ";
  17.   DIALOGBOXWINDOW_CLASS dbw(hwnd);
  18.   switch(message){
  19.     case WM_INITDIALOG:
  20.       for(i = j = 0; i < 6; i++){
  21.         if(faces[i] / 10) string[0] = '0' + faces[i] / 10;
  22.         string[1] = '0' + faces[i] % 10;
  23.         dbw.ListboxAdd(IDD_List, string);
  24.         if(die == faces[i]) j = i;
  25.       }
  26.       dbw.ListboxSetsel(IDD_List, j);
  27.       dbw.CheckRadio(IDD_One, IDD_Four, IDD_One + dice - 1);
  28.       dbw.SetInt(IDD_Times, times);
  29.       if(times < 10 || times > 10000) dbw.DisableOK();
  30.       return TRUE;
  31.     case WM_COMMAND:
  32.       switch(wparam){
  33.         case IDD_Times:
  34.           i = dbw.GetInt(IDD_Times);
  35.           if(i < 10 || i > 10000) dbw.DisableOK();
  36.           else dbw.EnableOK();
  37.           return TRUE;
  38.         case IDOK:
  39.           die = faces[dbw.ListboxGetsel(IDD_List)];
  40.           for(i = IDD_One; i < IDD_Four; i++) if(dbw.IsButtonChecked(i)) break;
  41.           dice = i - IDD_One + 1;
  42.           times = dbw.GetInt(IDD_Times);
  43.           test = 1;
  44.           dbw.End(1);
  45.           return TRUE;
  46.         case IDCANCEL:
  47.           dbw.End(0);
  48.           return TRUE;
  49.       }
  50.       return FALSE;
  51.   }
  52.   return FALSE;
  53. }
  54.  
  55. DBFUNC HelpAbout(HWND hwnd, WORD message, WORD wparam, LONG lparam){
  56.   HBITMAP hbm;
  57.   RECT rect;
  58.   BITMAP_CLASS bc;
  59.   BASEWINDOW_CLASS bwc;
  60.   DIALOGBOXWINDOW_CLASS dbw(hwnd);
  61.   switch(message){
  62.     case WM_INITDIALOG:
  63.       return TRUE;
  64.     case WM_PAINT:
  65.       bwc.Set(dbw.GetItem(IDD_Bitmap));
  66.       bwc.Invalidate(TRUE);
  67.       bwc.Update();
  68.       bwc.GetClientRect(&rect);
  69.       hbm = hinst.LoadBitmap("ILLINOIS");
  70.       bc.Get(bwc.Get());
  71.       bc.Setup(hbm);
  72.       bc.Stretch(rect.right, rect.bottom);
  73.       bc.Cleanup();
  74.       DeleteObject(hbm);
  75.       bc.Release();
  76.       return FALSE;
  77.     case WM_COMMAND:
  78.       switch(wparam){
  79.         case IDOK:
  80.           dbw.End(1);
  81.           return TRUE;
  82.       }
  83.       return FALSE;
  84.   }
  85.   return FALSE;
  86. }
  87.  
  88. int FileRead(HWND hwnd){
  89.   int i = FALSE;
  90.   if(-1 == (fc.Open(fc.Getfilename(), OF_READ | OF_REOPEN))){
  91.     fc.FileMessage(hwnd, MF_READERROR);
  92.     return FALSE;
  93.   }
  94.   if(fc.ReadWORD() != WNDEXID) fc.FileMessage(hwnd, MF_TYPEERROR);
  95.   else{
  96.     test = fc.ReadBYTE();
  97.     die = fc.ReadBYTE();
  98.     dice = fc.ReadBYTE();
  99.     times = fc.ReadWORD();
  100.     fc.ReadBuffer((LPSTR)rolls, sizeof(rolls));
  101.     i = TRUE;
  102.   }
  103.   fc.Close();
  104.   return i;
  105. }
  106.  
  107. int FileWrite(HWND hwnd, int flag){
  108.   if(flag) if(IDNO == fc.FileMessage(hwnd, MF_REPLACE)) return FALSE;
  109.   if(-1 == (fc.Open(fc.Getfilename(), OF_CREATE | OF_REOPEN))){
  110.     fc.FileMessage(hwnd, MF_WRITEERROR);
  111.     return FALSE;
  112.   }
  113.   fc.WriteWORD(WNDEXID);
  114.   fc.WriteBYTE(test);
  115.   fc.WriteBYTE(die);
  116.   fc.WriteBYTE(dice);
  117.   fc.WriteWORD(times);
  118.   fc.WriteBuffer((LPSTR)rolls, sizeof(rolls));
  119.   fc.Close();
  120.   return TRUE;
  121. }
  122.  
  123. WNDPROC WndProc(HWND hwnd, UINT message, UINT wparam, LONG lparam){
  124.   int i, j, k, l;
  125.   float x, y;
  126.   RECT rect;
  127.   BRUSHPEN_CLASS bc;
  128.   DIALOGBOX_CLASS dbc(hinst.Get(), hwnd);
  129.   DIALOGBOXWINDOW_CLASS dbw(hwnd);
  130.   switch(message){
  131.     case WM_CREATE:
  132.       randomize();
  133.       return FALSE;
  134.     case WM_DESTROY:
  135.       PostQuitMessage(0);
  136.       return FALSE;
  137.     case WM_CLOSE:
  138.       DestroyWindow(hwnd);
  139.       return FALSE;
  140.     case WM_PAINT:
  141.       if(IsIconic(hwnd) == NULL){
  142.         bc.Begin(hwnd);
  143.         bc.SelectSolid(WHITE);
  144.         bc.Rectangle();
  145.         bc.SelectDelete();
  146.         if(test){
  147.           dbw.GetClientRect(&rect);
  148.           j = (die - 1) * dice + 1;
  149.           for(i = k = 0; i < j; i++) if(rolls[i] > k) k = rolls[i];
  150.           x = (float)(rect.right - rect.left) / j;
  151.           y = (float)(rect.bottom - rect.top) / k;
  152.           bc.SelectSolid(BLUE);
  153.           if(x < 2) l = 0;
  154.           else if(x < 4) l = 1;
  155.           else l = 2;
  156.           for(i = 0; i < j; i++) bc.Rectangle(rect.left + i * x + l,
  157.            rect.top + (k - rolls[i]) * y, rect.left + wmax(i * x + l + 1,
  158.            (i + 1) * x - l), rect.top + k * y);
  159.           bc.SelectDelete();
  160.         }
  161.         bc.End();
  162.       }
  163.       return FALSE;
  164.     case WM_COMMAND:
  165.       switch(wparam){
  166.         case IDM_FileOpen:
  167.       fc.Setcaption("File Open");
  168.       fc.Setdefaultspec("*.wex");
  169.       i = fc.FileOpen(hwnd);
  170.       if(i){
  171.             i = FileRead(hwnd);
  172.             if(i) test = 2;
  173.             else test = 0;
  174.             dbw.Invalidate();
  175.             dbw.SendMessage(WM_PAINT);
  176.           }
  177.           return FALSE;
  178.         case IDM_FileSave:
  179.           if(test != 1) return FALSE;
  180.         case IDM_FileSaveas:
  181.           if(test == 0) return FALSE;
  182.       fc.Setcaption("File Save as");
  183.       fc.Setdefaultspec("*.wex");
  184.       fc.Getfilespec()[0] = 0;
  185.       i = fc.FileSaveas(hwnd);
  186.       if(i){
  187.             if(i != 2) i = 0;
  188.             i = FileWrite(hwnd, i);
  189.             if(i) test = 2;
  190.           }
  191.           return FALSE;
  192.         case IDM_FileExit:
  193.           dbw.SendMessage(WM_CLOSE);
  194.           return FALSE;
  195.         case IDM_Runtest:
  196.           i = dbc.DialogBox((FARPROC)RunTest, "RUNTESTDB");
  197.           if(i){
  198.             for(i = 0; i < 100; i++) rolls[i] = 0;
  199.             for(i = 0; i < times; i++){
  200.               for(j = k = 0; j < dice; j++) k += random(die) + 1;
  201.               rolls[k - dice]++;
  202.             }
  203.             dbw.Invalidate();
  204.             dbw.SendMessage(WM_PAINT);
  205.           }
  206.           return FALSE;
  207.         case IDM_HelpAbout:
  208.           i = dbc.DialogBox((FARPROC)HelpAbout, "HELPABOUTDB");
  209.           return FALSE;
  210.       } break;
  211.     default: return DefWindowProc(hwnd, message, wparam, lparam);
  212.   }
  213. }
  214.  
  215. int PASCAL WinMain(HANDLE hinstance, HANDLE hprevinstance,
  216.  LPSTR lpszcmdparam, int ncmdshow){
  217.   int i;
  218.   static char applicationname[] = "wndex";
  219.   WND_CLASS wndclass(hinstance, applicationname, WndProc);
  220.   WINDOWACCEL_CLASS window(hinstance, applicationname);
  221.   wndclass.style(CS_HREDRAW | CS_VREDRAW);
  222.   wndclass.LoadIcon();
  223.   wndclass.lpszMenuName();
  224.   if(!hprevinstance) wndclass.Register();
  225.   fc.Setapplicationname("Wndlib example");
  226.   hinst.Set(hinstance);
  227.   fc.Sethinstance(hinstance);
  228.   window.lpWindowName(fc.Getapplicationname());
  229.   window.Display();
  230.   window.LoadAccelerators();
  231.   while(window.TestMessage()) { window.AccelMessage(); }
  232.   return window.GetmsgwParam();
  233. }